Skip to content

Fix connect ECONNREFUSED error when trying to debug on iOS Sim #2704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 11, 2017

Conversation

rosen-vladimirov
Copy link
Contributor

@rosen-vladimirov rosen-vladimirov commented Apr 7, 2017

When trying to debug on iOS Simulator (via NativeScript Inspector), we often receive Error: connect ECONNREFUSED 127.0.0.1:18181 error.
There are two different problems. First one is incorrect identifying of the application's PID - we are using _.trimStart method totally incorrectly and in case your application identifier has numbers in it, they'll also be replaced in the search PID string. For example in case your app id is org.nativescript.app10, and the real PID of the running application is 18129, the current usage of trimStart method will give you the PID 8129.
Fix this by applying regular expression and find the correct PID.

The second problem is that there's some delay between opening the NativeScript Inspector and the debugged application on the device. In many cases the first time when we try to connect, we receive the error ECONNREFUSED. However in the previous implementation, we were trying to connect multiple times. Get back the old code and adjust it to work with Promises instead of the old sync execution.

Second commit:

Fix debug command:

  • tns debug ios prints message that you have to open null url in Chrome - when you do not pass --chrome to this command, it will use NativeScript inspector, so hide the incorrect message.
  • debugData is constructed too early in the command - in case the application has not been built before calling tns debug ..., construction of debugData will fail as CLI will not be able to find the latest built package. In order to fix this make the pathToAppPackage not mandatory (we do not need it for debug commands when --start is passed) and populate it after successful deploy of the application. This way it will have correct value. Delete most of the DebugDataService as most of the methods are not needed with these changes.
  • remove the check if --chrome is passed in order to print the url when tns debug android is used. The check was incorrect (it should check the value of options.client), but in fact we do not need it - the idea of the check was to suppress starting of Node Inspector, however we do not start it anymore no matter of the passed flags. So remove the incorrect check.

When trying to debug on iOS Simulator (via NativeScript Inspector), we often receive `Error: connect ECONNREFUSED 127.0.0.1:18181` error.
There are two different problems. First one is incorrect identifying of the application's PID - we are using `_.trimStart` method totally incorrectly and in case your application identifier has numbers in it, they'll also be replaced in the search PID string. For example in case your app id is `org.nativescript.app10`, and the real PID of the running application is 18129, the current usage of `trimStart` method will give you the PID 8129.
Fix this by applying regular expression and find the correct PID.

The second problem is that there's some delay between opening the NativeScript Inspector and the debugged application on the device. In many cases the first time when we try to connect, we receive the error ECONNREFUSED. However in the previous implementation, we were trying to connect multiple times. Get back the old code and adjust it to work with Promises instead of the old sync execution.
});

frontendSocket.on("close", () => {
console.log("frontend socket closed");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be $logger

}
});
backendSocket.on("close", () => {
console.log("backend socket closed");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be $logger

@@ -103,11 +103,18 @@ class IOSDebugService extends DebugServiceBase implements IPlatformDebugService

let lineStream = byline(child_process.stdout);
this._childProcess = child_process;
const pidRegExp = new RegExp(`${debugData.applicationIdentifier}:\\s?(\\d+)`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have Unit Tests for this Regular Expression. I'd suggest we extract this to some method/service so that we can test it and cover with more expressions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure where's the appropriate place for this code. I agree that it needs tests and I'll add them in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind - I've created a new helper method and added unit tests for it: telerik/mobile-cli-lib#942

const pidMatch = lineText.match(pidRegExp);

if (!pidMatch) {
this.$logger.trace(`Line ${lineText} does not contain the searched pattern: ${pidRegExp}.`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there's no pidMatch, what happens with const pid = pidMatch[1] below? Won't an undefined exception occur?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've forgotten to commit a return here 😄 Thanks for pointing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@rosen-vladimirov rosen-vladimirov force-pushed the vladimirov/fix-econnreset-emulator branch from 16f506b to 7ab62ce Compare April 10, 2017 10:47

protected printDebugInformation(information: string[]): void {
if (this.$options.chrome) {
_.each(information, i => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call super.printDebugInformation here instead of the each

- `tns debug ios` prints message that you have to open `null` url in Chrome - when you do not pass `--chrome` to this command, it will use NativeScript inspector, so hide the incorrect message.
- debugData is constructed too early in the command - in case the application has not been built before calling `tns debug ...`, construction of debugData will fail as CLI will not be able to find the latest built package. In order to fix this make the `pathToAppPackage` not mandatory (we do not need it for debug commands when `--start` is passed) and populate it after successful deploy of the application. This way it will have correct value. Delete most of the DebugDataService as most of the methods are not needed with these changes.
- remove the check if `--chrome` is passed in order to print the url when `tns debug android` is used. The check was incorrect (it should check the value of `options.client`), but in fact we do not need it - the idea of the check was to suppress starting of Node Inspector, however we do not start it anymore no matter of the passed flags. So remove the incorrect check.
@rosen-vladimirov rosen-vladimirov force-pushed the vladimirov/fix-econnreset-emulator branch from 7ab62ce to 8180c70 Compare April 10, 2017 15:16
@rosen-vladimirov
Copy link
Contributor Author

ping @yyosifov - all comments are fixed

Move the logic for getting PID of application started on iOS Simulator to mobile-cli-lib.
Add new helper method to get the process ID of an application from iOS Simulator Logs. Whenever we start an application on iOS Simulator, in its logs we can find the PID of the process in the following format:
```
<app id>: <app id>: <PID>
```
Add unit tests for the method.
@rosen-vladimirov rosen-vladimirov force-pushed the vladimirov/fix-econnreset-emulator branch from 72619c0 to df7b887 Compare April 10, 2017 21:26
@rosen-vladimirov rosen-vladimirov merged commit 99c9bf5 into master Apr 11, 2017
@rosen-vladimirov rosen-vladimirov deleted the vladimirov/fix-econnreset-emulator branch April 11, 2017 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants